package com.guang.springbootswagger.controller;

import com.guang.springbootswagger.bean.Employee;
import com.guang.springbootswagger.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * swagger属性的详细说明
 */
@RestController
@RequestMapping("employees")
@Api(value = "用户资源",tags = "用户资源控制器")
// @Api：用在类上，说明该类的作用，页面上显示的是只是tags中的值
public class EmployeeController {

    @ApiOperation(value = "查询所有用户")
    @GetMapping
    public List<Employee> list(){
        return Arrays.asList(new Employee(1L,"dafei", 18),new Employee(2L,"xiaofei", 16));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "昵称",name = "name",dataType = "String",required = true),
            @ApiImplicitParam(value = "年龄",name = "age",dataType = "int",required = true)
    })

    @ApiOperation(value = "添加新员工")
    @PostMapping("/add")
    public Employee add2(String name, int age){
        return new Employee(1L, name, age);
    }


    /**
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询指定ID的值")
    @GetMapping("/detail")
    public Employee detail(Long id){
        return new Employee(id, "dafei", 18);
    }

    @ApiOperation(value = "根据id来删除员工")
    @DeleteMapping
    public JsonResult delete(Long id){
        System.out.println("删除了.....");
        return JsonResult.success();
    }

    @ApiOperation(value = "更新员工")
    @PutMapping
    public Employee update(Employee employee){
        employee.setName(employee.getName() +"_update");
        return employee;
    }

    /**
     * @ApiOperation：用在方法上，说明方法的作用
     * value显示在上面，notes显示在下面
     * @param employee
     * @return
     */
    @ApiOperation(value = "添加员工",notes = "新增员工")
    @PostMapping
    public Employee add(Employee employee){
        employee.setId(1L);
        return employee;
    }
}